package vrp;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import vrp.SOrder;
import vrp.Customer;
import vrp.PMF;

public class SOrderEditServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {

    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
        
        try {
        	long sorderID = Integer.parseInt(req.getParameter("sorder"));
        	long customerID = Integer.parseInt(req.getParameter("customer"));
        	int recurringType = Integer.parseInt(req.getParameter("recurringType"));
            Date startDate = df.parse(req.getParameter("startDate"));
            Date endDate = df.parse(req.getParameter("endDate"));
            int sorderStatus = Integer.parseInt(req.getParameter("sorderStatus"));

            Key sorderKey = KeyFactory.createKey(SOrder.class.getSimpleName(), sorderID);
            SOrder sorder = pm.getObjectById(SOrder.class, sorderKey);
            Key customerKey = KeyFactory.createKey(Customer.class.getSimpleName(), customerID);
            //Customer customer = pm.getObjectById(Customer.class, customerKey);
            
            sorder.setCustomer(customerKey);
            sorder.setRecurringType(recurringType);
            sorder.setStartDate(startDate);
            sorder.setEndDate(endDate);
            sorder.setSOrderStatus(sorderStatus);
        } catch (ParseException e) {
			e.printStackTrace();
		} finally {
            pm.close();
        }
        resp.sendRedirect("/sorders.jsp");
    }
}